package client.comm.baoding.fragment

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.comm_ui.draw.SpaceItemBottomDecoration
import client.comm.octfdsmall.widget.CustomViewPager1
import client.comm.baoding.R
import client.comm.baoding.adapter.CatePagingAdapter
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.databinding.FragmentCatechildBinding
import client.comm.baoding.ui.vm.MainViewModel
import kotlinx.coroutines.flow.collect

class CateNewChild2Fragment(var mViewPager1: CustomViewPager1) :
    BaseFragment<FragmentCatechildBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_catechild
    }

    val adapter: CatePagingAdapter by lazy {
        CatePagingAdapter(requireContext(), mImgLoader).apply {

              /*refreshLoading = {
                  if (itemCount == 0) {
                      binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                  }
              }
              refreshNotLoading = {
//                  binding.refreshLayout.isRefreshing = false
                  if (itemCount == 0) {
                      binding.emptyLayout.errorType = EmptyLayout.NODATA

                  } else {
                      binding.emptyLayout.visibility = View.GONE
                  }
              }
              refreshError = {
                  if (itemCount == 0)
                      binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
//                  toast((it).error.message)
              }*/
        }
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    override fun initWidget() {
//        binding.vm = viewModel
        binding.recyclerView.apply {
            adapter = this@CateNewChild2Fragment.adapter

//            val gridLayoutManager = GridLayoutManager(requireContext(), 2)
            val gridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            layoutManager = gridLayoutManager
            addItemDecoration(
                SpaceItemBottomDecoration(
                    10
                )
            )

//            onLoadMore = {
//                this@CateNewChildFragment.adapter.retry()
//            }
        }

        binding.emptyLayout.setOnClickListener {
            this@CateNewChild2Fragment.adapter.refresh()

        }
        /*binding.refreshLayout.setOnRefreshListener {
            this@CateNewChildFragment.adapter.refresh()
        }
        */

        arguments?.let {
            val types: CateGoods.TypeInfo? = it.getParcelable<CateGoods.TypeInfo>("bean")
            types?.let {
                lifecycleScope.launchWhenCreated {
                    viewModel.cateChild2Flow(it.category_id).collect {
                        adapter.submitData(it)
                    }
                }
            }

            val fragment_ID = it.getInt("fragmentID")
            mViewPager1.setObjectForPosition(rootView, fragment_ID)
        }


    }
}

